﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WSNs
{
    public partial class CtlAcqConfig : UserControl
    {
        public Action<AcqStructure, ChannelType> AcqConfigEvent;
        public Action<double,bool> AcqTimeConfigEvent;  //采集时间事件
        private AcqStructure _acqStruct = new AcqStructure();
        private StructBytes _structBytes = new StructBytes();
        public CtlAcqConfig()
        {
            InitializeComponent();
        }

        private void CtlAcqConfig_Load(object sender, EventArgs e)
        {
            cb_couplingmode.SelectedIndex = 1;
            cb_measuerrange.SelectedIndex = 0;
            cb_samplingmode.SelectedIndex = 0;
            cb_samplingrate.Text = "100";
            cb_samplingrate.Enabled = true;
            SamplingUint.Text = "Hz";

            cb_storage.SelectedIndex = 0;
            cb_nodeType.SelectedIndex = 0;
            cb_length.SelectedIndex = 0;
        }

        private void cb_samplingmode_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cb_samplingmode.SelectedIndex)
            {
                case 0:
                    tb_trigger.Enabled = false;
                    tb_length.Enabled = false;
                    cb_length.Enabled = false;
                    SamplingUint.Text = "Hz";
                    break;
                case 1:
                    tb_trigger.Enabled = false;
                    tb_length.Enabled = true;
                    cb_length.Enabled = true;
                    SamplingUint.Text = "Hz";
                    break;
                case 2:
                    tb_trigger.Enabled = true;
                    tb_length.Enabled = false;
                    cb_length.Enabled = false;
                    SamplingUint.Text = "Hz";
                    break;
                case 3:
                    tb_trigger.Enabled = true;
                    tb_length.Enabled = true;
                    cb_length.Enabled = true;
                    SamplingUint.Text = "Hz";
                    break;
                case 4:     //等角度采样
                    cb_samplingrate.Enabled = true;
                    tb_length.Enabled = true;
                    cb_length.Enabled = true;
                    tb_nodeID.Enabled = false;
                    SamplingUint.Text = "Points/r";
                    break;
            
            }
        }

        private void btn_acq_ok_Click(object sender, EventArgs e)
        {
            try
            {
                //_acqStruct.AcqMode = (AcqMode)(cb_samplingmode.SelectedIndex+1);
                _acqStruct.NodeID = byte.Parse(tb_nodeID.Text);
                _acqStruct.FileID = byte.Parse(tb_fileID.Text);
                _acqStruct.CoupleMode = (CoupleMode)(cb_couplingmode.SelectedIndex + 1);
                if (cb_samplingmode.SelectedIndex == 4)
                {
                    _acqStruct.AcqMode = (AcqMode)(cb_samplingmode.SelectedIndex + 3);
                   
                }
                else
                {
                    _acqStruct.AcqMode = (AcqMode)(cb_samplingmode.SelectedIndex + 1);                
                }
                //_acqStruct.Frequency = float.Parse(cb_samplingrate.Text);
                _acqStruct.Frequency = UInt32.Parse(cb_samplingrate.Text);
                if(cb_length.SelectedIndex == 0)
                {
                    _acqStruct.AcqLength = (uint)(Convert.ToDouble(tb_length.Text.ToString()) * _acqStruct.Frequency * 3); //采样时间乘采样频率，再乘3，等于采样的字节数
                }
                else
                {
                    _acqStruct.AcqLength = UInt32.Parse(tb_length.Text);
                }
                

                _acqStruct.MeasuerRange = (byte) (cb_measuerrange.SelectedIndex);
                _acqStruct.TrigerV = byte.Parse(tb_trigger.Text);
                _acqStruct.StorageLocation = (StorageLocation) (cb_storage.SelectedIndex + 1);

                ChannelType chn = ChannelType.SingleChannel;
                switch (cb_nodeType.SelectedIndex)
                {
                    case 0://通用振动采集节点
                        chn = ChannelType.SingleChannel;
                        break;
                    case 1://四通道采集节点
                        chn = ChannelType.FourChannels;
                        break;
                    case 2://低功耗小节点
                        chn = ChannelType.SingleChannelSmall;
                        break;
                }
                AcqConfigEvent(_acqStruct, chn);

                if (cb_samplingmode.Text == "定长采样") //失能启动、停止采集按钮
                {
                    if(cb_length.SelectedIndex == 0)
                    {
                        AcqTimeConfigEvent(Convert.ToDouble(tb_length.Text.ToString()) * 3, false);
                    }
                    else
                    {
                        double acq_length = Convert.ToDouble(tb_length.Text.ToString());
                        double acq_fre = Convert.ToDouble(cb_samplingrate.Text.ToString());
                        AcqTimeConfigEvent(acq_length / acq_fre, false);
                    }
                    
                }
                else
                {
                    AcqTimeConfigEvent(0, true);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message,"参数错误");
            }
        }
    }
}
